﻿using System;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using WebApplication11.Dao;
using WebApplication11.Services;

namespace WebApplication11.Controllers;

public class tpController : Controller
{
    private tpService _tpService = new tpService();
    // GET
    
    public bool Auth()
    {
        if (HttpContext.Session.GetInt32("Status") == 1)
        {
            return true;
        }
        return false;
    }
    
    public IActionResult Index()
    {
        if (!Auth())
        {
            return RedirectToAction("Index", "Home");
        }
        if (HttpContext.Session.GetInt32("Insert") != 1)
        {
            ViewBag.Insert = 0;
        }
        var count = getIdentity();
        if (count != 0)
        {
            ViewBag.Identity = count;
        }
        else
        {
            ViewBag.Identity = 0;
        }

        ViewBag.username = HttpContext.Session.GetString("user");
        ViewBag.msg = _tpService.getMsg();
        return View();
    }
    
    public int getIdentity()
    {
        var res = _tpService.getIdentity(HttpContext.Session.GetString("user"));
        return res.Count;
    }

    public IActionResult delete(string tree, string park)
    {
        _tpService.delete(tree, park);
        return RedirectToAction("Index");
    }
    
    public ActionResult addNew(String tree, String park)
    {
        ViewBag.Insert = 0;
        HttpContext.Session.Remove("Insert");
        _tpService.insert(tree, park);
        return RedirectToAction("Index");
    }
    
    public ActionResult insertNewAssets(String tree, String park)
    {
        ViewBag.Insert = 1;
        HttpContext.Session.SetInt32("Insert", 1);
        return RedirectToAction("Index");
    }
}